package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo3 {//
    //创建Callable任务对象
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<String>call=new MyCallable();
//   FutureTask作用1:是Runnable对象(实现了Runnable接口)，这样就可以交给Thread创建出一个线程了
//         FutureTask作用2:可以在线程执行完毕之后调用get方法拿到该线程的返回值
        FutureTask<String> task = new FutureTask<>(call);
        Thread thread=new Thread(task);
        thread.start();
        System.out.println(task.get());//这里会等待MyCallable线程执行完成后 再拿结果 主线程也会停止在这
        System.out.println("hello world");
    }
}
class MyCallable implements Callable<String>{

    @Override
    public String call() throws Exception {
        Thread.sleep(3000);
        String s1="wo";
        s1=s1+"xi";
        s1=s1+"huan";
        s1=s1+"ni";
        return s1;
    }
}
